package com.jp.knowledge.adapter.team_circle;


import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.adapter.OnItemListerner;
import com.jp.knowledge.model.TeamCircleConstactsModel;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;
import com.jp.knowledge.util.ImageLoadUtil;
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class TeamCircleConstactsAdapter extends RecycleVAdapter<TeamCircleConstactsModel>implements
        StickyRecyclerHeadersAdapter<RecycleVHolder>, CompoundButton.OnCheckedChangeListener, Comparator<TeamCircleConstactsModel>, View.OnClickListener {

    private boolean showCheckBox;

    private OnItemListerner  onItemListerner;

    public TeamCircleConstactsAdapter(Context context, @NonNull List<TeamCircleConstactsModel> list) {
        super(context, list);
        showCheckBox = true;
    }

    public void setOnItemListerner(OnItemListerner onItemListerner) {
        this.onItemListerner = onItemListerner;
    }

    public void setShowCheckBox(boolean isShow){
        this.showCheckBox = isShow;
    }

    @Override
    public int compare(TeamCircleConstactsModel o1, TeamCircleConstactsModel o2) {
        if(o1 == null || o2 == null || o1.getRemark() == null || o2.getRemark() == null)
            return -1;
        return o1.getRemark().compareTo(o2.getRemark());
    }

    @Override
    public void updateAdapter(@NonNull List<TeamCircleConstactsModel> list) {
        Collections.sort(list, this);
        mList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.team_circle_contacts_item;
    }

    @Override
    public void onBind(RecycleVHolder holder, int position) {
        holder.getConvertView().setTag(position);
        holder.getConvertView().setOnClickListener(this);
        ImageLoadUtil.load(mContext,
                mList.get(position).getUserIcon() + "?imageView2/2/w/100/interlace/1",
                holder.getImageView(R.id.team_circle_contacts_head),
                R.mipmap.ic_my_header
        );
        setTextView(holder.getTextView(R.id.team_circle_contacts_name), mList.get(position).getUserName());
        String detailText = mList.get(position).getPosition();
        if(detailText == null || detailText.trim().length() == 0) {
            detailText = mList.get(position).getDepartment();
        } else if(mList.get(position).getDepartment() != null && mList.get(position).getDepartment().trim().length() > 0){
            detailText += ("-" + mList.get(position).getDepartment());
        }
        setTextView(holder.getTextView(R.id.team_circle_contacts_detail), detailText);
        setTextView(holder.getTextView(R.id.phone), mList.get(position).getPhone());
        CheckBox checkBox = holder.get(R.id.team_circle_contacts_cb);
        if(this.showCheckBox){
            checkBox.setVisibility(View.VISIBLE);
            checkBox.setTag(position);
            checkBox.setChecked(mList.get(position).getIsChecked());
            ((CheckBox)holder.get(R.id.team_circle_contacts_cb)).setOnCheckedChangeListener(this);
        } else{
            checkBox.setVisibility(View.GONE);
        }
    }

    /* 悬浮头部 */
    @Override
    public long getHeaderId(int position) {
        return getData(position).getRemark().charAt(0);
    }
    @Override
    public RecycleVHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_remark_header, parent, false);
        return new RecycleVHolder(view);
    }
    @Override
    public void onBindHeaderViewHolder(RecycleVHolder holder, int position) {
        holder.setTextView(R.id.remark_header_tv, getData(position).getRemark().charAt(0) + "");
    }

    @Override
    public void onClick(View v) {
        CheckBox checkBox = (CheckBox) v.findViewById(R.id.team_circle_contacts_cb);
        checkBox.setChecked(!checkBox.isChecked());

        if(onItemListerner!=null){
            int position = (int) v.getTag();
            onItemListerner.onItemClick(v,position);
        }
    }

    protected void setTextView(TextView textView, String text){
        if(text == null || text.trim().length() == 0){
            textView.setVisibility(View.GONE);
        }else{
            textView.setVisibility(View.VISIBLE);
            textView.setText(text);
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(mList.get((int) buttonView.getTag()).getIsChecked() == isChecked)
            return;
        if(this.onItemSelectedListener != null)
            this.onItemSelectedListener.onItemSelect((int) buttonView.getTag(), isChecked);
    }
    private OnItemSelectedListener onItemSelectedListener;
    public void setOnItemSelectedListener(OnItemSelectedListener onItemSelectedListener){
        this.onItemSelectedListener = onItemSelectedListener;
    }
    public interface OnItemSelectedListener{
        void onItemSelect(int position, boolean isChecked);
    }
}
